package com.faf.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.faf.dao.BaseDao;
import com.faf.tools.DbUtils;

public class BaseDaoImpl implements BaseDao
{
	protected PreparedStatement ps = null;
	protected Connection con = null;
	protected ResultSet rs = null;
	
	public BaseDaoImpl(){
		
	};
	
	public BaseDaoImpl(Connection con)
	{
		this.con = con;
	}
	
	private void initPreparedStatement(String sql, Object... objs) throws SQLException
	{
		if(con == null)
		{
		    con = DbUtils.getConnection();
		}
		
		ps = con.prepareStatement(sql);
		
		for (int i = 0; i < objs.length; i++)
		{
			ps.setObject(i+1, objs[i]);
		}
	}
	
	public int executeUpdate(String sql, Object... objs) throws SQLException
	{
		initPreparedStatement(sql,objs);
		return ps.executeUpdate();
	}
	

	public void executeQuery(String sql, Object... objs) throws SQLException
	{
		initPreparedStatement(sql,objs);
		
		rs = ps.executeQuery();
	}
	
	protected void close()
	{
		DbUtils.close(con, ps, rs);
	}
	
}
